<?php
require 'db.php';
$admin = $_POST['admin'];
$password = $_POST['password'];
session_start();
$strsql = "select * from tb_admin where ad_name= '$admin' and ad_pwd = '$password'";

$result = mysqli_query($link, $strsql);
$data = mysqli_fetch_array($result);


if(isset($_POST['login'])){
    //将表单输入字符转成 字符串
    
    //验证账号的正确性
    if(($admin=='') || ($password=='')){
        if(empty($admin))
        {
            echo "<script>alert('用户名不能为空！请重新输入！');</script>";
            header('refresh:0;url=index.php');
            exit;
        }
        else if(empty($password)){
            echo "<script>alert('密码不能为空！请重新输入！');</script>";
            header('refresh:0;url=index.php');
            exit;
        }
    }
    else if (($admin!=$data['ad_name']) || ($password!=$data['ad_pwd'])){
        echo "<script>alert('用户名或密码错误，请重新输入！');</script>";
        header('refresh:0;url=index.php');
    }
    else if (($admin==$data['ad_name']) && ($password==$data['ad_pwd'])){
        $_SESSION['admin']=$admin;
        $_SESSION['islogin']=1;
        //7天内自动登录
        if ($_POST['remember']=='yes'){
            setcookie("admin",$admin,time+7*24*60*60);
            setcookie("code",md5($admin.md5($password)),time()+7*24*60*60);
        }
        else{
            setcookie("admin",'',time()-1);
            setcookie("code",'',time()-1);
        }
        header("location: manager.php");
        mysqli_free_result($result);
        mysqli_close($link);
    }
}
require 'index.html';
?>